import { loginAPI } from '@/api/user'
import { TOKEN_KEY } from '@/constants/KEY'
import { getToken, removeToken, setToken } from '@/utils/auth'

export default {
  namespaced: true,
  state: {
    token: getToken() || ''

  },
  mutations: {
    setToken(state, newToken) {
      state.token = newToken
      setToken(newToken)
    },
    removeToken(state) {
      state.token = ''
      removeToken(TOKEN_KEY)
    }

  },
  actions: {
    async login(context, { username, password }) {
      const res = await loginAPI({ username, password })
      context.commit('setToken', res.data.token)
    }

  }
}
